package com.zx.erp.repo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zx.erp.purchase.bean.CDetails;
import com.zx.erp.purchase.bean.CPurchase;
import com.zx.erp.purchase.mapper.CDetailsMapper;
import com.zx.erp.purchase.mapper.CPurchaseMapper;
import com.zx.erp.purchase.service.CDetailsService;
import com.zx.erp.purchase.service.CPurchaseService;
import com.zx.erp.repo.bean.KcWarehouse;
import com.zx.erp.repo.bean.RkWarehouse;
import com.zx.erp.repo.mapper.KcWarehouseDao;
import com.zx.erp.repo.mapper.RkWarehouseDao;
import com.zx.erp.repo.service.KcWarehouseBiz;
import com.zx.erp.repo.service.RkWarehouseBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 入库Biz接口实现类
 *
 * @author Jerry
 */
@Service
public class RkWarehouseBizImpl implements RkWarehouseBiz {
    @Autowired
    private RkWarehouseDao rkDao;

    @Autowired
    private CPurchaseService cPurchaseBiz;

    @Autowired
    private KcWarehouseBiz kcWarehouseBiz;
    @Autowired
    private CDetailsService cDetailsBiz;

    @Override
    public PageInfo<RkWarehouse> selectAllDetails(String cName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RkWarehouse> rkWarehouses = rkDao.selectAllDetails(cName);
        PageInfo<RkWarehouse> rkWarehousePageInfo = new PageInfo<>(rkWarehouses);
        return rkWarehousePageInfo;
    }

    @Override
    public PageInfo<RkWarehouse> selectAll(Integer provinceId, Integer cityId, String cName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RkWarehouse> rkWarehouseList = rkDao.selectAll(provinceId, cityId, cName);
        PageInfo<RkWarehouse> rkWarehousePageInfo = new PageInfo<>(rkWarehouseList);
        return rkWarehousePageInfo;
    }

    @Override
    public PageInfo<RkWarehouse> findAll(String cName, String rkIndent, String state, Integer warehouseId, Integer pageno, Integer pagesize) {
        PageHelper.startPage(pageno, pagesize);
        List<RkWarehouse> allRk = rkDao.findAllRk(cName, rkIndent, state, warehouseId);
        PageInfo<RkWarehouse> pin = new PageInfo<>(allRk);

        return pin;
    }

    @Override
    public RkWarehouse findById(Integer id) {
        return rkDao.findById(id);
    }

    @Override
    public Boolean saveRk(RkWarehouse rkWarehouse) {
        return rkDao.insertRk(rkWarehouse);
    }

    @Override
    public Boolean addRk(RkWarehouse rkWarehouse) {
        // 修改采购状态为入库
        CPurchase cPurchase = cPurchaseBiz.findByPid(rkWarehouse.getRkIndent());
        cPurchase.setCgState(2);
        cPurchase.setWarehuoseId(rkWarehouse.getWarehouseId());
        cPurchaseBiz.update(cPurchase);
        return rkDao.insertRk(rkWarehouse);
    }

    @Override
    public Boolean update(RkWarehouse rkWarehouse) {
        return rkDao.update(rkWarehouse);
    }

    @Override
    public Boolean chengeCgStatus(String purchaseId) {
        // 修改采购和入库的  入库状态
        CPurchase purchase = cPurchaseBiz.findByPid(purchaseId);
        RkWarehouse rkWarehouse = this.findAll(null, purchaseId, null, null, 1, 2).getList().get(0);
        purchase.setCgState(purchase.getCgState() == 1 ? 2 : 1);  //1(入库) 2(未入库) 状态
        rkWarehouse.setState(rkWarehouse.getState().equals("1") ? "2" : "1");
        Boolean flag = null;
        if (purchase.getCgState() == 1) {  //入库添加仓库信息
            CDetails details = cDetailsBiz.findByPid(purchaseId);

            KcWarehouse kc = new KcWarehouse();
            kc.setWarehouseId(purchase.getWarehuoseId());
            kc.setBrandId(details.getBrandId());
            kc.setTypeId(details.getTypeId());
            kc.setProductId(details.getProductId());
            kc.setFactoryId(details.getFirmId());
            kc.setRepertory(details.getCount());
            flag = kcWarehouseBiz.saveKc(kc);

        } else if (purchase.getCgState() == 2) {  //取消入库则删除仓库信息
            CDetails details = cDetailsBiz.findByPid(purchaseId);

            KcWarehouse kc = new KcWarehouse();
            kc.setWarehouseId(purchase.getWarehuoseId());
            kc.setBrandId(details.getBrandId());
            kc.setTypeId(details.getTypeId());
            kc.setProductId(details.getProductId());
            kc.setFactoryId(details.getFirmId());
            flag = kcWarehouseBiz.deleteKc(kc);
        }
        Boolean update = cPurchaseBiz.update(purchase);
        Boolean update1 = this.update(rkWarehouse);
        return update1;
    }

}
